package com.maotai.llzhou.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

@Component("huaRunDao")
@Transactional
public class HuaRunDao extends CommonDao{
	
	public Map<String, Object> getUniqueId(Map<String, String> map)throws SQLException{
		Map<String, Object> uniqueIdMap = this.getSqlSession().selectOne("com.maotai.llzhou.dao.HuaRunMapper.getUniqueId",map);
		map.put("uniqueId", uniqueIdMap.get("uniqueId")+"");
		this.getSqlSession().insert("com.maotai.llzhou.dao.HuaRunMapper.insertUserUniqueCheck",map);
        return uniqueIdMap;
    }
	public Map<String, Object> getAppVersion()throws SQLException{
		Map<String, Object> uniqueIdMap = this.getSqlSession().selectOne("com.maotai.llzhou.dao.HuaRunMapper.getAppVersion");
        return uniqueIdMap;
    }
	
	public Map<String, Object> getHROrder()throws SQLException{
        return this.getSqlSession().selectOne("com.maotai.llzhou.dao.HuaRunMapper.getHROrder");
    }
	public int insertQQOrder(Map<String, String> map)throws SQLException{
		return this.getSqlSession().insert("com.maotai.llzhou.dao.HuaRunMapper.insertQQOrder",map);
	}
	public int updateHROrder(Map<String, String> map)throws SQLException{
		return this.getSqlSession().insert("com.maotai.llzhou.dao.HuaRunMapper.updateHROrder",map);
	}
	public int insertHROrder(Map<String, String> map)throws SQLException{
		return this.getSqlSession().insert("com.maotai.llzhou.dao.HuaRunMapper.insertHROrder",map);
	}
	public int updateUserOrderCheck(Map<String, String> map)throws SQLException{
		return this.getSqlSession().insert("com.maotai.llzhou.dao.HuaRunMapper.updateUserOrderCheck",map);
	}
	public int deleteUser(Map<String, String> map)throws SQLException{
		return this.getSqlSession().insert("com.maotai.llzhou.dao.HuaRunMapper.deleteUser",map);
	}
	public int uploadUser(List<Map<String, String>> userList)throws SQLException{
		if(userList!=null && userList.size()>0){
			for(Map<String, String> userMap : userList){
				Map<String, Object> userOne = this.getSqlSession().selectOne("com.maotai.llzhou.dao.HuaRunMapper.getOneUser",userMap);
				if(userOne!=null && userOne.size()>0){
					this.getSqlSession().update("com.maotai.llzhou.dao.HuaRunMapper.updateUser",userMap);
				}else{
					this.getSqlSession().insert("com.maotai.llzhou.dao.HuaRunMapper.uploadUser",userMap);
				}
				
			}
		}
		return 1;
	}
	public List<Map<String, Object>> getAllCity()throws SQLException{
		List<Map<String, Object>> cityList = this.getSqlSession().selectList("com.maotai.llzhou.dao.HuaRunMapper.getAllCity");
		if(cityList!=null && cityList.size()>0){
			for(int i=0;i<cityList.size();i++){
//			for(Map<String, Object> cityMap : cityList){
				Map<String, Object> cityMap = cityList.get(i);
				List<Map<String, Object>> storeList = this.getSqlSession().selectList("com.maotai.llzhou.dao.HuaRunMapper.getAllStore",cityMap);
				cityMap.put("children", storeList);
//				System.out.println(cityMap);
			}
		}
        return cityList;
    }
	public List<Map<String, Object>> queryMaotaiUser(Map<String,String> map)throws SQLException{
//		String sql=this.getSqlSession().getConfiguration().getMappedStatement("com.maotai.llzhou.dao.HuaRunMapper.queryMaotaiUser").getBoundSql(map).getSql();
//		System.out.println(sql);
		List<Map<String, Object>> userTempList = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> userList = this.getSqlSession().selectList("com.maotai.llzhou.dao.HuaRunMapper.queryMaotaiUser",map);
		if(userList!=null && userList.size()>0){
			
			for(Map<String,Object> userMap : userList){
				String[] citys = (userMap.get("codes")+"").split("#");
				if(citys!=null && citys.length>0){
					StringBuffer cityBuffer = new StringBuffer();
					for(String city : citys){
						cityBuffer.append("'"+city+"'");
						cityBuffer.append(",");
					}
					String cityStr = cityBuffer.toString();
					cityStr = cityStr.substring(0,cityStr.length()-1);
					map.put("cityStr", cityStr);
					//查询出城市
					List<Map<String, Object>> cityList = this.getSqlSession().selectList("com.maotai.llzhou.dao.HuaRunMapper.queryCitys",map);
					StringBuffer storeBuffer = new StringBuffer();
					if(cityList!=null && cityList.size()>0){
						String cityName = "";
						for(Map<String, Object> cityMap : cityList){
							cityName = cityMap.get("cityName")+"";
							storeBuffer.append(cityMap.get("storeName")+"|");
						}
						String storeStr = cityBuffer.toString();
						storeStr = storeBuffer.substring(0,storeBuffer.length()-1);
						userMap.put("cityName", cityName);
						userMap.put("storeStr", storeStr);
					}
				}
				String buyContent = userMap.get("buyContent")+"";
				if(buyContent!=null && !"".equals(buyContent) && !"null".equals(buyContent)){
					JSONObject resultObj = JSONObject.parseObject(buyContent).getJSONObject("data");
					if(buyContent.contains("buyCount")){
						userMap.put("result", "可以购买 "+resultObj.getIntValue("buyCount")+" 瓶");
					}else{
						userMap.put("result", resultObj.getString("result"));
					}
				}
				userTempList.add(userMap);
			}
		}
		return userTempList;
    }
	public List<Map<String, Object>> queryHuaRunOrderSimple(Map<String,String> map)throws SQLException{
        return this.getSqlSession().selectList("com.maotai.llzhou.dao.HuaRunMapper.queryHuaRunOrderSimple",map);
    }
	public List<Map<String, Object>> queryHuaRunOrderItem(Map<String,String> map)throws SQLException{
        return this.getSqlSession().selectList("com.maotai.llzhou.dao.HuaRunMapper.queryHuaRunOrderItem",map);
    }
	public List<Map<String, Object>> queryQQOrder()throws SQLException{
        
        List<Map<String, Object>> qqOrderList = this.getSqlSession().selectList("com.maotai.llzhou.dao.HuaRunMapper.queryQQOrder");
        if(qqOrderList!=null && qqOrderList.size()>0){
        	for(Map<String, Object> qqOrderMap : qqOrderList){
        		this.getSqlSession().delete("com.maotai.llzhou.dao.HuaRunMapper.deleteQQOrder",qqOrderMap);
        	}
        }
        return qqOrderList;
	}
	public List<Map<String, Object>> getZhanghaoInfo()throws SQLException{
        List<Map<String, Object>> zhanghaoList = this.getSqlSession().selectList("com.maotai.llzhou.dao.HuaRunMapper.getZhanghaoInfo");
        return zhanghaoList;
	}
	public List<Map<String, Object>> queryOneQQOrder()throws SQLException{
        
        List<Map<String, Object>> qqOrderList = this.getSqlSession().selectList("com.maotai.llzhou.dao.HuaRunMapper.queryOneQQOrder");
        if(qqOrderList!=null && qqOrderList.size()>0){
        	for(Map<String, Object> qqOrderMap : qqOrderList){
        		this.getSqlSession().delete("com.maotai.llzhou.dao.HuaRunMapper.deleteQQOrder",qqOrderMap);
        	}
        }
        return qqOrderList;
	}
}
